package com.java.sample.domain;

import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.util.Objects;

@Slf4j
public class AdSystemEnvInfoServiceImpl implements IAdInfoService {

    @Override
    public String getUserName(){
        return System.getProperty(CommonConsts.PROP_USER_NAME);
    }

    @Override
    public String getComputerName() {
        try {
            // 通用方法（所有平台）
            String hostname = InetAddress.getLocalHost().getHostName();
            if (Objects.nonNull(hostname) && !hostname.isEmpty()) {
                return hostname;
            }
        } catch (Exception e) {
            log.warn("Error getting hostname via InetAddress: ", e.getMessage());
        }

        // 平台回退策略
        if (System.getProperty("os.name").toLowerCase().contains("win")) {
            return System.getenv(CommonConsts.PROP_WIN_COMPUTER_NAME);
        } else {
            return System.getenv(CommonConsts.PROP_LINUX_COMPUTER_NAME);
        }
    }

    @Override
    public String getDomain(){
        return System.getProperty(CommonConsts.PROP_DOMAIN);
    }

}
